package cn.huaqingcheng.tool.http;

import cn.huaqingcheng.tool.core.StrTool;
import cn.huaqingcheng.tool.core.StringPool;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;

/**
 * HTTP 响应
 **/
@SuppressWarnings("ClassNamePrefixedWithPackageName")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(fluent = true, chain = true)
public class HttpResponse<B> {

    /**
     * HTTP 版本
     */
    private HttpVersion version;

    /**
     * Http 响应状态
     */
    private Integer status;

    /**
     * Http 状态描述
     */
    private String statusMsg;

    /**
     * 响应头
     */
    private HttpHeaders header;

    /**
     * 响应体
     */
    private B body;

    @Override
    public String toString() {
        return toString(true, true);
    }

    public @NotNull String toString(boolean showHeader, boolean showBody) {
        StringBuilder sb = new StringBuilder();
        sb.append(version).append(StringPool.SPACE)
                .append(status).append(StringPool.SPACE).append(statusMsg);

        if (header.isNotEmpty() && showHeader) {
            sb.append(StrTool.LINE_BREAK)
                    .append(header);
        }
        if (body != null && showBody) {
            sb.append(StrTool.LINE_BREAK)
                    .append(body);
        }
        return sb.toString();
    }

}
